/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.awt.BorderLayout;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
import sys.bean.ClienteBean;
import sys.dao.ClienteDao;
import sys.utils.ConexaoADS;
import sys.utils.LetterUpper;
import sys.utils.Mensagens;
import sys.utils.Utils;
import static sys.utils.Utils.codCorreto;
import static sys.utils.Utils.escClose;

/**
 *
 * @author Administrador
 */
public class UICobrancaCliente extends javax.swing.JDialog {

    ClienteBean cliente;
    SimpleDateFormat formDataSpac = new SimpleDateFormat("yyyyMMdd");

    /**
     * Creates new form UIGerarCobrancaCliente
     *
     * @param parent
     * @param modal
     */
    public UICobrancaCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        dataInicial.setDate(new Date());
        dataFinal.setDate(new Date());
        UIManager.put("Table.alternateRowColor", new java.awt.Color(230, 255, 255));
        escClose(this);
        txCodigoCliente.requestFocus();
        Utils.maximizarDialog(this);
    }

    private void gerarCobranca() {

        HashMap parametro = new HashMap();
//        parametro.put("data_inicial", formDataSpac.format(dataInicial.getDate()));
        parametro.put("data_vencimento", formDataSpac.format(dataFinal.getDate()));
        parametro.put("cod_cliente", "%" + txCodigoCliente.getText() + "%");
        parametro.put("SUBREPORT_DIR", "sys/report/");

        if (cbPago.isSelected()) {
            parametro.put("situacao", "P");
        }
        if (cbPendente.isSelected()) {
            parametro.put("situacao", "A");
        }

        if (cbPago.isSelected() && cbPago.isSelected()) {
            parametro.put("situacao", "R");
        }
//        SimpleDateFormat formato = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");;
//        Date data = new Date();
//        String dataFinal = formato.format(data);
//        String nomeRelatorio = "Relatorio_" + dataFinal + ".pdf";
        jpCobranca.removeAll();
        jpCobranca.setLayout(new BorderLayout());
        InputStream diretoryReport = getClass().getClassLoader().getResourceAsStream("sys/report/cliente.jasper");
        try {
            JasperPrint jasperPrint = JasperFillManager.fillReport(diretoryReport, parametro, ConexaoADS.getConection());
            JRViewer jasperViewer = new JRViewer(jasperPrint);
            jpCobranca.add(jasperViewer);
            jpCobranca.revalidate();
            jpCobranca.repaint();

        } catch (SQLException | IOException | JRException ex) {
            Mensagens.msgErro("erro ao gerar cobranca\n" + ex.getMessage());
        }

    }

    public void preencheCampos() {
        try {
            cliente = ClienteDao.consultaCliente(codCorreto(txCodigoCliente.getText(), 5));
            txCodigoCliente.setText(cliente.getCodigo());
            txNomeCliente.setText(cliente.getNome());

        } catch (SQLException | IOException ex) {
            Mensagens.msgErro("erro ao carregar dados do cliente\n" + ex.getMessage());
        }
    }

    private void loadSearch() {
        UIPesquisaCliente pCliente = new UIPesquisaCliente(this, true);
        pCliente.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jpDados = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dataInicial = new com.toedter.calendar.JDateChooser("dd/MM/yyyy", "##/##/#####", '_');
        dataFinal = new com.toedter.calendar.JDateChooser("dd/MM/yyyy", "##/##/#####", '_');
        jpDados1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txCodigoCliente = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        txNomeCliente = new javax.swing.JTextField();
        lbRazao = new javax.swing.JLabel();
        jpDados2 = new javax.swing.JPanel();
        cbPendente = new javax.swing.JCheckBox();
        cbPago = new javax.swing.JCheckBox();
        jpCobranca = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        btnBuscar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jpDados.setBackground(new java.awt.Color(255, 255, 255));
        jpDados.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PERÍODO:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel2.setText("DATA INICIAL:");

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel3.setText("DATA FINAL:");

        dataInicial.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        dataFinal.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        javax.swing.GroupLayout jpDadosLayout = new javax.swing.GroupLayout(jpDados);
        jpDados.setLayout(jpDadosLayout);
        jpDadosLayout.setHorizontalGroup(
            jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDadosLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(dataInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(dataFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpDadosLayout.setVerticalGroup(
            jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDadosLayout.createSequentialGroup()
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dataInicial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dataFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jpDados1.setBackground(new java.awt.Color(255, 255, 255));
        jpDados1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DADOS DO CLIENTE:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel4.setText("CODIGO:");

        txNomeCliente.setDocument(new LetterUpper());
        txCodigoCliente.setBackground(new java.awt.Color(245, 245, 245));
        txCodigoCliente.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txCodigoCliente.setForeground(new java.awt.Color(51, 51, 255));
        txCodigoCliente.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txCodigoCliente.setBorder(null);
        txCodigoCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txCodigoClienteActionPerformed(evt);
            }
        });
        txCodigoCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txCodigoClienteKeyReleased(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/find-2.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        txNomeCliente.setDocument(new LetterUpper());
        txNomeCliente.setEditable(false);
        txNomeCliente.setBackground(new java.awt.Color(245, 245, 245));
        txNomeCliente.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txNomeCliente.setForeground(new java.awt.Color(51, 51, 255));
        txNomeCliente.setBorder(null);

        lbRazao.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        lbRazao.setText("NOME:");

        javax.swing.GroupLayout jpDados1Layout = new javax.swing.GroupLayout(jpDados1);
        jpDados1.setLayout(jpDados1Layout);
        jpDados1Layout.setHorizontalGroup(
            jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDados1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpDados1Layout.createSequentialGroup()
                        .addComponent(lbRazao)
                        .addGap(0, 381, Short.MAX_VALUE))
                    .addComponent(txNomeCliente))
                .addContainerGap())
        );
        jpDados1Layout.setVerticalGroup(
            jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDados1Layout.createSequentialGroup()
                .addGroup(jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpDados1Layout.createSequentialGroup()
                        .addGroup(jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbRazao))
                        .addGap(1, 1, 1)
                        .addGroup(jpDados1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton1))
                .addGap(0, 9, Short.MAX_VALUE))
        );

        jpDados2.setBackground(new java.awt.Color(255, 255, 255));
        jpDados2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "OPÇÕES:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N

        cbPendente.setBackground(new java.awt.Color(255, 255, 255));
        cbPendente.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbPendente.setSelected(true);
        cbPendente.setText("PENDENTE");

        cbPago.setBackground(new java.awt.Color(255, 255, 255));
        cbPago.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbPago.setSelected(true);
        cbPago.setText("PAGO");

        javax.swing.GroupLayout jpDados2Layout = new javax.swing.GroupLayout(jpDados2);
        jpDados2.setLayout(jpDados2Layout);
        jpDados2Layout.setHorizontalGroup(
            jpDados2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDados2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpDados2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbPendente)
                    .addComponent(cbPago))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpDados2Layout.setVerticalGroup(
            jpDados2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDados2Layout.createSequentialGroup()
                .addComponent(cbPendente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbPago, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(14, 14, 14))
        );

        jpCobranca.setBackground(new java.awt.Color(255, 255, 255));
        jpCobranca.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        javax.swing.GroupLayout jpCobrancaLayout = new javax.swing.GroupLayout(jpCobranca);
        jpCobranca.setLayout(jpCobrancaLayout);
        jpCobrancaLayout.setHorizontalGroup(
            jpCobrancaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jpCobrancaLayout.setVerticalGroup(
            jpCobrancaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 424, Short.MAX_VALUE)
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        btnBuscar.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/find.png"))); // NOI18N
        btnBuscar.setText("GERAR");
        btnBuscar.setPreferredSize(new java.awt.Dimension(120, 30));
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        jPanel4.add(btnBuscar);

        btnSair.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/sair.png"))); // NOI18N
        btnSair.setText("SAIR");
        btnSair.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jPanel4.add(btnSair);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpCobranca, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jpDados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jpDados1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jpDados2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jpDados, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpDados1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpDados2, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpCobranca, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txCodigoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txCodigoClienteActionPerformed
        if (txCodigoCliente.getText().equals("")) {
            loadSearch();
        } else {
            preencheCampos();
        }
    }//GEN-LAST:event_txCodigoClienteActionPerformed

    private void txCodigoClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txCodigoClienteKeyReleased
        if (txCodigoCliente.getText().equals("")) {
            txNomeCliente.setText("");
        }
    }//GEN-LAST:event_txCodigoClienteKeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        loadSearch();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed

        final BarraProgresso pb = new BarraProgresso(this);
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                pb.gerarJanela();
                gerarCobranca();
                return null;
            }

            @Override
            protected void done() {
                pb.fechar();
            }
        };
        worker.execute();
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnSair;
    private javax.swing.JCheckBox cbPago;
    private javax.swing.JCheckBox cbPendente;
    private com.toedter.calendar.JDateChooser dataFinal;
    private com.toedter.calendar.JDateChooser dataInicial;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jpCobranca;
    private javax.swing.JPanel jpDados;
    private javax.swing.JPanel jpDados1;
    private javax.swing.JPanel jpDados2;
    private javax.swing.JLabel lbRazao;
    public javax.swing.JTextField txCodigoCliente;
    public javax.swing.JTextField txNomeCliente;
    // End of variables declaration//GEN-END:variables
}
